package com.wangan;

import com.itextpdf.kernel.pdf.*;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) {
        String current = System.getProperty("user.dir");
        System.out.println(String.format("current=%s", current));
        File file = Paths.get(current, "java1.8_win.pdf").toFile();
        if (!file.exists()) {
            System.out.println(String.format("file not exist,file=%s", file.getAbsoluteFile()));
            return;
        }

        try {
            PdfReader reader = new PdfReader(file);
            PdfWriter writer = new PdfWriter(Paths.get(current, "java1.8_new.pdf").toFile());

            PdfDocument document = new PdfDocument(reader, writer);
            int count = document.getNumberOfPages();
            if (count == 0) {
                return;
            }

            PdfPage first = document.getPage(1);
            // 确定第 1 页的注释就是需要删除的
            first.remove(PdfName.Annots);

            // 修改信息
            PdfDocumentInfo info = document.getDocumentInfo();
            System.out.println("old info");
            System.out.println(String.format("author=%s", info.getAuthor()));
            System.out.println(String.format("creator=%s", info.getCreator()));
            System.out.println(String.format("producer=%s", info.getProducer()));
            System.out.println(String.format("subject=%s", info.getSubject()));
            System.out.println(String.format("title=%s", info.getTitle()));
            System.out.println();

            info.setAuthor("");
            info.setCreator("");
            // NOTE 并不能真实清空 PDF 制作者信息
            info.setProducer("");
            info.setSubject("");

            document.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
